<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/29
 * Time: 18:39
 */

namespace Xcx\Controller;

use Xcx\Common\ErrorCode;
use Xcx\Model\GoodsBargainModel;
use Xcx\Model\GoodsOrderModel;

header("Content-type: text/html; charset=utf-8");
class OrderController extends BaseController
{

    public function test()
    {
        dump('http://'.$_SERVER['HTTP_HOST'].__APP__.'/Xcx/WxJsAPI/');
    }

    /**
     * 获取确认订单信息
     */
    public function getConfirmOrderInfo()
    {
        $orderNo = I('order_no');
        $GoodsOrder = new GoodsOrderModel();
        $data = $GoodsOrder->getConfirmOrderInfo($orderNo);
        $data['code'] = 0;
        $this->ajaxReturn($data);
    }

    /**
     * 更新订单默认选中的地址
     */
    public function updateOrderAddrID()
    {
        $orderNo = I('order_no');
        $addrID = I('addr_id');
        $GoodsOrder = new GoodsOrderModel();
        $res = $GoodsOrder->updateOrderAddrID($orderNo, $addrID);
        if ($res) {
            $arr = array('code'=>0);
        } else {
            $arr = array('code'=>ErrorCode::$Fail,'msg'=>'更新失败');
        }
        $this->ajaxReturn($arr);
    }

    /**
     * 添加订单砍价记录
     */
    public function addBargainInfo()
    {
        $orderNo = I('order_no');

        $GoodsBargain = new GoodsBargainModel();
        $res = $GoodsBargain->addBargainInfo($orderNo);
        if ($res) {
            $arr = array('code'=>0);
        } else {
            $arr = array('code'=>ErrorCode::$Fail,'msg'=>'砍价订单生成失败');
        }
        $this->ajaxReturn($arr);
    }

    public function doAddOrder()
    {
        $idList = I('id_list');
        $idList = htmlspecialchars_decode($idList);
        $idList = json_decode($idList,true);
        $GoodsOrder = new GoodsOrderModel();
        $res = $GoodsOrder->doAddOrder($idList);
        if ($res) {
            $arr = array('code'=>0,'order_no'=>$res);
        } else {
            $arr = array('code'=>ErrorCode::$Fail,'msg'=>'订单生成失败');
        }
        $this->ajaxReturn($arr);
    }

    /**
     * 发起支付
     */
    public function pay()
    {
        $orderNo = I('order_no');
        $type = I('dis_type',0);
        $discountID = I('discount_id',0);
        $addrID = I('addr_id');
        $freight = I('freight');
        $gid = I('gid');
        $msg = I('msg','');

        $GoodsOrder = new GoodsOrderModel();
        $res = $GoodsOrder->pay($orderNo,$type,$discountID,$addrID,$freight,$gid,$msg);
        if ($res) {
            $arr = array('code'=>0,'jsApiParameters'=>$res);
        } else {
            $arr = array('code'=>ErrorCode::$Fail,'msg'=>'信息处理失败');
        }

        $this->ajaxReturn($arr);
    }

    /**
     * 重新付款
     */
    public function rePay()
    {
        $orderNo = I('order_no');

        $GoodsOrder = new GoodsOrderModel();
        $res = $GoodsOrder->rePay($orderNo);
        if ($res) {
            $arr = array('code'=>0,'jsApiParameters'=>$res);
        } else {
            $arr = array('code'=>ErrorCode::$Fail,'msg'=>'信息处理失败');
        }
        $this->ajaxReturn($arr);
    }

    /**
     * 获取订单列表
     */
    public function getOrderList()
    {
        $state = I('state','all');

        $GoodsOrder = new GoodsOrderModel();
        $list = $GoodsOrder->getOrderList($state);
        $arr = array('code'=>0,'list'=>$list);
        $this->ajaxReturn($arr);
    }

    /**
     * 更新订单状态
     */
    public function updateOrderState()
    {
        $orderNo = I('order_no');
        $state = I('state');

        $stateArr = array('4','5');
        $res = false;
        if (in_array($state,$stateArr)) {
            $GoodsOrder = new GoodsOrderModel();
            $res = $GoodsOrder->updateOrderState($orderNo, $state);
        }

        if ($res) {
            $arr = array('code'=>0,'jsApiParameters'=>$res);
        } else {
            $arr = array('code'=>ErrorCode::$Fail,'msg'=>'信息处理失败');
        }

        $this->ajaxReturn($arr);
    }
}